/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rgenerator;

import java.io.File;
import java.util.ArrayList;

/**
 *
 * @author alex2stf <alex2stf@yahoo.com>
 */
public class DirectoryReader {
    
    
    
    private  String ClassNamePrefix;

    private File parentFolder;
    private String caleAbsoluta;
    
    private ArrayList<PackageMaker> pachete =new ArrayList<>();

    public ArrayList<PackageMaker> getPachete() {
        return pachete;
    }
    
    
    private PackageMaker currentpack;
    
    private String numePachet;
    
    private String numeDirector;
    private String mainFile;
    private Stringers type;
    
    public DirectoryReader(File file, String numePachet, String mainFile, String classNamePrefix, Stringers type){
        this.type=type;
        this.ClassNamePrefix=classNamePrefix;
        this.numePachet=numePachet;
        this.mainFile=mainFile;
        
        parentFolder=file;
        caleAbsoluta =parentFolder.getParentFile().toURI().toString();
        System.out.println("parent" +  caleAbsoluta);

        numeDirector="R";//file.getName();
        currentpack=new PackageMaker(numeDirector, numePachet, mainFile, ClassNamePrefix, type);
        pachete.add(currentpack);
        denumireClasa="R";
        Proceseaza(file);   

    }
    
    
    
    
    
    
    
    
    
    private int nrDeFis;
    private int cnt=0;
    private ArrayList<String> listaDenumiri=new ArrayList<>();
    private String denumireClasa;
    private String numeParent="";
    
    private void Proceseaza(File aFile){
   
      
        
        for (int i = 0; i < aFile.listFiles().length; i++) {
           if (aFile.listFiles()[i].isFile()) {
                
                String caleTemporara=aFile.listFiles()[i].toURI().toString();
                String caleRelativa=caleTemporara.replaceAll(caleAbsoluta, "");
                String numeVariabila=aFile.listFiles()[i].getName().toString();
                String fileNameWithOutExt = numeVariabila.replaceFirst("[.][^.]+$", "");
                
                
//                    for (int j = 0; j < aFile.listFiles().length; j++) {
//                        if (aFile.listFiles()[j].isFile()) {
//                             String numVarTemp=aFile.listFiles()[j].getName().toString();
//                             String filWitExt= numVarTemp.replaceFirst("[.][^.]+$", "");
//                            if (fileNameWithOutExt.equals(filWitExt)) {
//                                fileNameWithOutExt=numeVariabila.replaceAll("[.]","_");
//                            }
//                        }
//                    }
                
                
                currentpack.addVars(fileNameWithOutExt, caleRelativa, numeVariabila);  
            }
        }
        
        //inchide fisierul adaugand nume de clasa la fiecare director
        currentpack.close(aFile.listFiles(), caleAbsoluta);
        
        for (int j = 0; j < aFile.listFiles().length; j++) {
            
                    if (aFile.listFiles()[j].isDirectory()) {
                        
                        String caleTemporaraD=aFile.listFiles()[j].toURI().toString();//toata calea
                        
                        String caleRelativaD=caleTemporaraD.replaceAll(caleAbsoluta, "");//caleaFaraAbs
                        denumireClasa=Stringers.convertPathToClassName(caleRelativaD);
                        
                        System.out.println(">>>>"+denumireClasa);             
                        listaDenumiri.add(denumireClasa);
                        currentpack=new PackageMaker(denumireClasa, numePachet, mainFile, ClassNamePrefix, type);
                        pachete.add(currentpack);
                        //System.out.println(denumireClasa);
                        Proceseaza(aFile.listFiles()[j]);
                    }
        }
    }
    
    
    
    
    
    //verifica daca e unic in lista numele de clasa generat din numele de folder
    private Boolean isCheckUnique(ArrayList<String> paramList, String param ){
        for (int i = 0; i < paramList.size(); i++) {
            if (paramList.get(i).equals(param)) {
                return true;
            }
        }
        return false;
    }

    
    
    
    
    
    
    
  
}
